<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>Redis subscribe SSE test</title>

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<button id="helloBtn">euraca</button>
<div class="container-fluid">
  <div class="row">
    <div class="col-md-8 col-md-offset-2">

      <p class="lead">SSE stream</p>

      <p>Once you loaded this page, push something to one of the Redis channels subscribed</p>

      <ul class="list-unstyled"></ul>

    </div>
  </div>
</div>

<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/eureca.js"></script>

<script type="text/javascript">
  var socket = io('http://localhost:5000');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });

  $(function() {
    var source = new EventSource('/stream');
    var $list = $('ul');
    // when channelsAsEvents is true, you can listen to specific events...
    source.addEventListener('stock', handleMessage, false);
    source.addEventListener('user', handleMessage, false);

    // otherwise, you can simply listen to generic messages
    source.onmessage = handleMessage;
    function handleMessage(e) {
      console.log(e);
      $message = $('<li>').html('<strong>' + e.type + '</strong>: ' + e.data);
      $list.append($message);
    }
  });


  var eurecaClient = new Eureca.Client();

  //export the method to be called from the server
  eurecaClient.exports.clientEcho = function(msg) {
    console.log(msg);
  }

  //eureca client cannot issue remote calls before ready event.
  //we need to wait for it
  eurecaClient.ready(function(serverProxy) {
    var btn = document.getElementById('helloBtn');

    btn.onclick = function() {
      serverProxy.serverEcho('Hello from client');
    }
  });

</script>

</body>
</html>
